package com.heima.behavior.service.impl;

import com.heima.behavior.service.BehaviorService;
import com.heima.common.exception.CustomException;
import com.heima.common.redis.CacheService;
import com.heima.model.behavior.dtos.LikesBehaviorDto;
import com.heima.model.behavior.dtos.ReadBehaviorDto;
import com.heima.model.behavior.dtos.UnLikesBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.BehaviorThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class BehaviorServiceImpl implements BehaviorService {

    @Autowired
    private CacheService cacheService;

    /**
     * 点赞
     * @param dto
     * @return
     */
    public ResponseResult like(LikesBehaviorDto dto){
        System.out.println("dto:"+dto);

        if (dto.getArticleId()==null||dto.getOperation()==null||dto.getType()==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        //检查参数
        if (dto.getType()<0||dto.getType()>2||dto.getOperation()<0||dto.getOperation()>1){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        ApUser user = BehaviorThreadLocalUtil.getUser();
        String key="leadnews:like:"+user.getId();

        if (dto.getOperation()==0){
            //点赞
            if(cacheService.sIsMember(key,dto.getArticleId()+"")){
                throw new CustomException(AppHttpCodeEnum.HAS_LIKE);
            }

            cacheService.sAdd(key, dto.getArticleId() + "");
        }
        if (dto.getOperation()==1){
            //取消点赞
            cacheService.sRemove(key,dto.getArticleId()+"");
        }

        return new ResponseResult<>();
    }


    /**
     * 阅读
     * @param dto
     * @return
     */
    public ResponseResult read(ReadBehaviorDto dto){
        System.out.println("dto="+dto);

        //检查参数
        if (dto.getArticleId()==null||dto.getCount()==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getCount()<0){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        String key="leadnews:read:"+dto.getArticleId().toString()+"_read";
        int read=0;
        if (cacheService.exists(key)){
            //已有阅读数据
            String s = cacheService.get(key);
            read= Integer.parseInt(s);
        }
        read=read+dto.getCount();
        cacheService.set(key,read+"");

        return  ResponseResult.okResult(0,"");
    }

    /**
     * 不喜欢
     */
    public ResponseResult unlike(UnLikesBehaviorDto dto){
        if (dto.getArticleId()==null||dto.getType()==null){
            throw new CustomException(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        if (dto.getType()<0||dto.getType()>1){
            throw new CustomException(AppHttpCodeEnum.PARAM_INVALID);
        }

        //获取当前用户信息
        ApUser user = BehaviorThreadLocalUtil.getUser();
        String key="leadnews:unlike:"+user.getId();

        if (dto.getType()==0){
            //不喜欢
            cacheService.sAdd(key,dto.getArticleId()+"");
        }
        if (dto.getType()==1){
            //取消不喜欢
            cacheService.sRemove(key,dto.getArticleId()+"");
        }

        return new ResponseResult<>();

    }
}
